package com.business.business.common.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.business.business.common.base.BaseService;
import com.business.business.support.model.RespEntity;
import com.business.business.support.utils.requestutil.HttpRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import static com.business.business.common.utils.GlobalUtil.getRequest;

@Component
public class tokenUtil  {
    @Autowired
    private BaseService baseService;

    private static BaseService staticBaseService;

    @PostConstruct
    public void init() {
        staticBaseService = baseService;
    }

    //获得token
    public static com.alibaba.fastjson.JSONObject getToken(JSONObject jsonParam){
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用token验证接口
        com.alibaba.fastjson.JSONObject token = staticBaseService.makeDataForToken(jsonParam,"systemsetup","config","getDecodeToken","systemsetup");
        return token;
    }
    //判断token是否为空
    public static RespEntity choiceEmpty (com.alibaba.fastjson.JSONObject token){
        if(StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if(StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        return null;
    }
    //获取homeId
    public static String getHomeId(com.alibaba.fastjson.JSONObject token){
        return token.getString("home_id");
    }
    //获取userType
    public static String getUserType(com.alibaba.fastjson.JSONObject token){
        return token.getString("user_type");
    }
}
